#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Simple Joycon Controller Reader
简单的 Joycon 手柄读取器

Author: Yinchuan Li
Date: 2025-08-02
Description: 基于 pyjoycon 库的简单 Joycon 手柄输入读取
"""

from pyjoycon import JoyCon, get_R_id, get_L_id
import time

def main():
    """主函数"""
    print("🎮 Simple Joycon Controller Reader")
    print("按 Ctrl+C 退出程序")
    
    # 连接 Joycon
    joycon_L = None
    joycon_R = None
    
    # 连接左 Joycon
    try:
        joycon_id_L = get_L_id()
        if joycon_id_L and None not in joycon_id_L:
            joycon_L = JoyCon(*joycon_id_L)
            print("✅ 左 Joycon 连接成功")
    except Exception as e:
        print(f"❌ 左 Joycon 连接失败: {e}")
    
    # 连接右 Joycon
    try:
        joycon_id_R = get_R_id()
        if joycon_id_R and None not in joycon_id_R:
            joycon_R = JoyCon(*joycon_id_R)
            print("✅ 右 Joycon 连接成功")
    except Exception as e:
        print(f"❌ 右 Joycon 连接失败: {e}")
    
    if not joycon_L and not joycon_R:
        print("❌ 未检测到任何 Joycon 设备")
        return
    
    print("开始读取 Joycon 状态...")
    
    try:
        while True:
            # 读取左 Joycon
            if joycon_L:
                try:
                    status_L = joycon_L.get_status()
                    print("左 Joycon:", status_L)
                except Exception as e:
                    print(f"左 Joycon 读取失败: {e}")
            
            # 读取右 Joycon
            if joycon_R:
                try:
                    status_R = joycon_R.get_status()
                    print("右 Joycon:", status_R)
                except Exception as e:
                    print(f"右 Joycon 读取失败: {e}")
            
            print("-" * 50)
            time.sleep(0.1)
            
    except KeyboardInterrupt:
        print("\n程序已退出")
    finally:
        print("程序已完全退出")
        time.sleep(1)

if __name__ == "__main__":
    main()